/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.mediaplayerdemo;

import com.ryan.ohos.extension.util.WindowUtil;
import com.ryan.ohos.extension.widget.toolbar.Toolbar;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.webengine.ResourceRequest;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebView;

/**
 * WebAbility
 */
public class WebAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_web);

        WindowUtil.hideStatusBar(getWindow(), true);

        Toolbar toolbar = (Toolbar) findComponentById(ResourceTable.Id_nav);
        toolbar.setTitle(intent.getStringParam("title"));
        toolbar.setNavigationIcon(null);
        toolbar.setContentInsetStartWithNavigation(0);

        WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);

        webView.getWebConfig().setDataAbilityPermit(false);
        webView.setWebAgent(new WebAgent() {
            @Override
            public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
                return true;
            }
        });

        boolean url = intent.getBooleanParam("url", false);
        if (url) {
            webView.load(getString(ResourceTable.String_privacy_policy_url));
        } else {
            webView.load(getString(ResourceTable.String_licenses), "text/html", "utf-8", "about:blank", "");
        }
    }
}
